package com.kotlin.heimaplayer.ui.activity

import androidx.viewpager.widget.ViewPager
import com.kotlin.heimaplayer.R
import com.kotlin.heimaplayer.adapter.VideoPagerAdapter
import com.kotlin.heimaplayer.base.BaseActivity
import com.kotlin.heimaplayer.model.CommonResultBean
import com.kotlin.heimaplayer.presenter.impl.VideoPlayUrlPresenterImpl
import com.kotlin.heimaplayer.view.VideoPlayUrlView
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import kotlinx.android.synthetic.main.activity_video_player.*

class VideoPlayerActivity: BaseActivity(),VideoPlayUrlView {

    val presenter by lazy { VideoPlayUrlPresenterImpl(this) }

    override fun onError(msg: String?) {
        myToast("加载数据失败")
    }

    override fun onSuccess(result: CommonResultBean<String>) {
        val orientationUtils = OrientationUtils(this,videoView)
        videoView.setUp(result.data,true,"视频")
        videoView.startPlayLogic()
        videoView.backButton.setOnClickListener {

            this.finish()
        }
        videoView.fullscreenButton.setOnClickListener {
            orientationUtils.resolveByClick()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_video_player
    }

    override fun initData() {
        val vid = intent.getStringExtra("vid")
        println("vid是${vid}")
        vid?.let {
            presenter.getVideoPlayUrl(vid)
        }

    }

    override fun initListener() {
        // 适配viewpager
        viewPager.adapter = VideoPagerAdapter(supportFragmentManager)
        // radio切换
        rg.setOnCheckedChangeListener { radioGroup, i ->
            when(i){
                R.id.rb1->viewPager.setCurrentItem(0)
                R.id.rb2->viewPager.setCurrentItem(1)
                R.id.rb3->viewPager.setCurrentItem(2)
            }
        }
        viewPager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                when(position){
                    0->rg.check(R.id.rb1)
                    1->rg.check(R.id.rb2)
                    2->rg.check(R.id.rb3)
                }
            }

        })
    }
}